//修改名称_编辑
function toEditName(pid){
	var o_name = $("#proname_"+pid).data("proname");
	var txt = "<input type='text' class='ipt_img' id='ipt_proname_"+pid+"' value='"+o_name+"'/>";
	var o_txt = "<input type='hidden' class='ipt_img' id='o_ipt_proname_"+pid+"' value='"+o_name+"'/>";
	$("#proname_"+pid).html(txt);
	$("#proname_"+pid).append(o_txt);
	$("#saveimg_"+pid).attr("src","../img/operating_icon/icon23.png");
	$("#saveimg_"+pid).attr("onclick","doEditName('"+pid+"')");
}
//修改名称_提交
function doEditName(pid){
	var pattern=/[`~!@#$%^&?:"{},;'[\]]/im;
	var n_name = $("#ipt_proname_"+pid).val();
	var o_name = $("#o_ipt_proname_"+pid).val();
	if(n_name !="" && n_name != o_name && !pattern.test(n_name)){
		$.getJSON("ajax_operation.php",{id:pid,val:n_name,op:1},function(json){
			if(json.code == 1){
				$("#proname_"+pid).html(n_name);
				$("#proname_"+pid).data("proname",n_name);
			}else{
				$("#proname_"+pid).html(o_name);
				$("#proname_"+pid).data("proname",o_name);
			}
		});
	}else if(pattern.test(n_name)){
		alert('提示信息：您输入的数据含有非法字符！');
		$("#proname_"+pid).html(o_name);
	
	}
	if(n_name == o_name){
		$("#proname_"+pid).html(o_name);
	}
	
	$("#saveimg_"+pid).attr("src","../img/operating_icon/icon53.png");
	$("#saveimg_"+pid).attr("onclick","toEditName('"+pid+"')");
}
//////////////////////////////////////////////////列表中产品分类//////////////////////////////////////////////////
$(function(){
	$(".pro_typeimg").click(function(){
		var tleft = event.pageX;
		var ttop = event.pageY;
		$("#div_out").css("left",tleft+10+"px");
		$("#div_out").css("top",ttop+10+"px");
		var pid = $(this).data("pro-id");
		var ptid = $(this).attr("data-pro-typeid");
		var ptparent = $(this).data("pro-tparent");
		var pname = $("#proname_"+pid).data("proname");
		$("#span_out_title").html(pname);
		$("#div_out").find("input[name='types']").removeAttr("checked");
		var typeids = ptid.split(",");
		for(var i = 0 ; i < typeids.length ; i++){
			var tid = typeids[i];
			$("input[value='"+tid+"']").attr("checked","checked");
		}
		$("#hid_out_proid").val(pid);
		$("#div_out").show();
	});
	$("#div_inner").click(function(){
		$("#div_out").hide();
	});
	
	$("#btn_changetype").click(function(){
		$(this).attr("disabled","disabled");
		var pid = $("#hid_out_proid").val(); //产品编号
                var types = $("input[name='types']:checked");
		if(types.length == 0){
			alert("请选择类型！");
			$("#btn_changetype").removeAttr("disabled");			return;
		}
		var tids = "";
		var tname = "";
		types.each(function(i,n){
		   //if(i > 0){
			   tids+=",";
		   //}
		   tids += n.value;
		   var label = $(n).next("label").text();
		   tname += "/"+label;
	   });
	   tids += ","; 
		$.getJSON("ajax_operation.php",{type_id:tids,id:pid,op:3},function(json){
			alert(json.msg);
			if(json.code == 1){
				$("#protype_"+pid).html(tname);
				$("#savetypeimg_"+pid).attr("data-pro-typeid",tids);
			};
			$("#div_out").hide();
			$("#btn_changetype").removeAttr("disabled");
		});
	});
});
////////////////////////////////修改价格///////////////////////////////////////////////////////
$(function(){
	$(".pro_priceimg").click(function(){
		var tleft = event.pageX;
		var ttop = event.pageY; 
		$("#div_out2").css("left",tleft+10+"px");
		$("#div_out2").css("top",ttop+10+"px"); 
		
		var pid = $(this).data("pro-id"); //产品编号
		var pname = $("#proname_"+pid).data("proname");
		var price_o = $("#savepriceimg_"+pid).data("prooprice");
		var price_n = $("#savepriceimg_"+pid).data("pronprice");
		
		$("#span_price_title").html(pname);
//		$.getJSON("ajax_operation.php",{id:pid,val_o:price_o,val_n:price_n,op:5},function(json){
			var array = [{fpid:1,pid:1,proids:1,o_price:1,n_price:1,c_price:1,b_price:1,n_score:1}]//;eval(json);
			$("#WSY_t1").html("");
			var str = '';
			for (var i=0;i<array.length;i++){ 
				var apid = array[i]['fpid']; 
				var pid = array[i]['pid'];
				var proids = array[i]['proids'];
				var o_price = array[i]['o_price'];
				var n_price = array[i]['n_price'];
				var c_price = array[i]['c_price'];
				var b_price = array[i]['b_price'];
				var n_score = array[i]['n_score'];


				str = str + '<tr class="WSY_q1" data-tag="'+apid+'" >';
				str = str + '<td>'+proids+'</td>';
				str = str + '<td><input class="input_txprice" type="text" name="tx_prooprice" value="'+o_price+'" />元</td>';
				str = str + '<td><input class="input_txprice" type="text" name="tx_pronprice" value="'+n_price+'" />元</td>';
				str = str + '<td><input class="input_txprice" type="text" name="tx_probprice" value="'+b_price+'" />元</td>';
				str = str + '<td><input class="input_txprice" type="text" name="tx_procprice" value="'+c_price+'" />元</td>';
				str = str + '<td><input class="input_txprice" type="text" name="tx_pronscore" value="'+n_score+'" />分</td>';
				str = str + '</tr>';
			}
			$("#WSY_t1").append(str);
			$("#hid_price_proid").val(pid);  
			$("#div_out2").show();
			$(".input_txprice").on("keyup",function(){
				var val = $(this).val();
				val = val.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
				val = val.replace(/^\./g,""); //验证第一个字符是数字而不是
				val = val.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
				val = val.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
				val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
				$(this).val(val);
			});
//		});
	});
	$("#div_inner2").click(function(){
		$("#div_out2").hide();
	});
	$("#btn_changeprice").click(function(){
		var pid = $("#hid_price_proid").val(); 
		var o_price = $("input[name='tx_prooprice']");
		var n_price = $("input[name='tx_pronprice']");
		var b_price = $("input[name='tx_probprice']");
		var c_price = $("input[name='tx_procprice']");
		var n_score = $("input[name='tx_pronscore']");
		//console.log(o_price.val() +" == "+n_price.val()+" == "+b_price.val()+" == "+c_price.val()+" == "+n_score.val());
		//console.log(o_price.size() +" == "+n_price.size()+" == "+c_price.size()+" == "+n_score.size());

        var o_p = parseFloat(o_price.val()); //原价
        var n_p = parseFloat(n_price.val());//现价
        var b_p = parseFloat(b_price.val()); //成本价
        var c_p = parseFloat(c_price.val()); //供货价

		if(o_p < 0 ||  n_p < 0 ||  b_p < 0 ||  c_p < 0 ||  n_score.val() < 0  ){
			alert("不可为负数");
			return false;
		}
		
		if( o_p  < n_p ){
			alert("现价不可大于原价");
			return false;
		}
		if( n_p  < b_p ){
			alert("成本价不可大于现价");
			return false;
		}
		if( n_p  < c_p ){
			alert("供货价不可大于现价");
			return false;
		}
		if( b_p  < c_p && b_p !=0){			
			alert("供货价不可大于成本价");
			return false;
		}
		if(o_price.size() != n_price.size() ||  o_price.size() != c_price.size()){
			alert("数据异常！");
			return false;
		}
		var o_price_str = "";
		var n_price_str = "";
		var b_price_str = "";
		var c_price_str = "";
		var n_score_str = "";
		var n_fpid_str  = "";
		var check_price = 1 ; //1:允许执行 0,停止执行
		o_price.each(function(i,n){
			if(i > 0){
				o_price_str +=",";
				n_price_str +=",";
				b_price_str +=",";
				c_price_str +=",";
				n_score_str +=",";
				n_fpid_str += ",";
			}
			o_price_str += $(n).val();
			var fpid = $(n).parent().parent().data("tag");
			n_fpid_str  += fpid;
			n_price_str += $(n_price[i]).val();
			b_price_str += $(b_price[i]).val();
			c_price_str += $(c_price[i]).val();
			n_score_str += $(n_score[i]).val();
			if( $(n).val() < 0 || $(n_price[i]).val() < 0 || $(b_price[i]).val() < 0 || $(c_price[i]).val() < 0 || $(n_score[i]).val() < 0 ){
				alert("不可为负数");
				check_price = 0;
				return false;
			}
			/*console.log('$(n_price[i]).val()==='+$(n_price[i]).val());
			console.log('$(b_price[i]).val()==='+$(b_price[i]).val());
			console.log('$(c_price[i]).val()==='+$(c_price[i]).val());
			*/
			var o_p = parseFloat($(n).val()); //原价
            var n_p = parseFloat($(n_price[i]).val());//现价
            var b_p = parseFloat($(b_price[i]).val()); //成本价
            var c_p = parseFloat($(c_price[i]).val()); //供货价
			if( o_p  < n_p ){
				alert("现价不可大于原价"+i);
				check_price = 0;
				return false;
			}
			if( n_p < b_p ){
               // console.log("b_price : "+$(b_price[i]).val() +"  n_price : "+$(n_price[i]).val());
				alert("成本价不可大于现价"+i);
				check_price = 0;
				return false;
			}
			if( n_p < c_p ){
				alert("供货价不可大于现价"+i);
				check_price = 0;
				return false;
			}
			if( b_p < c_p && b_p !=0){
				alert("供货价不可大于成本价"+i);
				check_price = 0;
				return false;
			}
			
			//console.log(o_price_str +" == "+n_price_str +" == "+b_price_str+" == "+c_price_str+" == "+n_score_str);
		});	
		if(check_price == 0){
			return false;
		}
		$(this).attr("disabled","disabled");
		$.getJSON("ajax_operation.php",{id:pid,aids:n_fpid_str,val_os:o_price_str,val_ns:n_price_str,val_bs:b_price_str,val_cs:c_price_str,val_ss:n_score_str,op:6},function(json){
				if(json.code == 1){
					var price_o =  o_price[0].value;
					var price_n =  n_price[0].value;
					var price_b =  b_price[0].value;
					var price_c =  c_price[0].value;
					price_o = Math.round(price_o*100)/100;
					price_n = Math.round(price_n*100)/100;
					price_b = Math.round(price_b*100)/100;
					price_c = Math.round(price_c*100)/100;
					//+尹志雄----2017年3月3日10:35:15----禅道7825
					//修改$("# "+pid).html("现金:￥"+price_o);为$("#prooprice_"+pid).html("￥"+price_o);
					$("#prooprice_"+pid).html("￥"+price_o);
					$("#pronprice_"+pid).html("￥"+price_n);
					$("#pronprice_"+pid).attr("data-forprice",price_b);
					$("#savepriceimg_"+pid).data("prooprice",price_o);
					$("#savepriceimg_"+pid).data("pronprice",price_n);
					$("#pronprice_"+pid).data("costprice",price_c);
				};
			});
		$("#div_out2").hide();
		$("#btn_changeprice").removeAttr("disabled");
	});
});
//////////////////////////////////////////修改库存//////////////////////////////////////////
$(function(){
	$(".pro_stockimg").click(function(){
		var tleft = event.pageX;
		var ttop = event.pageY; 
		$("#div_out3").css("left",tleft+10+"px");
		$("#div_out3").css("top",ttop+10+"px");
		
		var pid = $(this).data("pro-id");
		var pname = $("#proname_"+pid).data("proname");
		var stock = $("#savestockimg_"+pid).data("prostock");
		
		$("#span_stock_title").html(pname);
//		$.getJSON("ajax_operation.php",{id:pid,val_s:stock,op:5},function(json){
			var array = [{'fpid':1,'pid':1,'proids':1,'s_num':1}];//eval(json);
			$("#WSY_t2").html("");
			var str = '';
			for (var i=0;i<array.length;i++){ 
				var apid = array[i]['fpid']; 
				var pid = array[i]['pid'];
				var proids = array[i]['proids'];
				var s_num = array[i]['s_num'];
				str = str + '<tr class="WSY_q2" data-tag="'+apid+'">';
				str = str + '<td>'+proids+'</td>';
				str = str + '<td><input class="input_txprock" type="text" name="tx_prostock" value="'+s_num+'" /></td>';
				str = str + '</tr>';
			}
			$("#WSY_t2").append(str);
			$("#hid_stock_proid").val(pid);  
			$("#div_out3").show();
			$(".input_txprock").on("keyup",function(){
				var val = $(this).val();
				val = val.replace(/\D/g,'');
				$(this).val(val);
			});
//		});
	});
	$("#div_inner3").click(function(){
		$("#div_out3").hide();
	});
        //保存
	$("#btn_changestock").click(function(){
		$(this).attr("disabled","disabled");
		var pid = $("#hid_stock_proid").val();
		var s_num = $("input[name='tx_prostock']");
		var s_num_str = "";
		var n_fpid_str = "";
		var error_num = 0;			//错误累计数
		s_num.each(function(i,n){
			if(i > 0){
				s_num_str +=",";
				n_fpid_str += ",";
			}
			if(parseInt($(n).val())<0){	//库存为负数
				error_num ++;
			}
			s_num_str += $(n).val();
			var fpid = $(n).parent().parent().data("tag");
			n_fpid_str  += fpid;
		});	
		if(error_num>0){
			alert('库存不能为负数');
			$("#btn_changestock").removeAttr("disabled");
			return;
		}
		
		$.getJSON("ajax_operation.php",{id:pid,aid:n_fpid_str,val_s:s_num_str,op:7},function(json){
			if(json.code == 1){
				var num_s =  s_num[0].value;
				$("#prostock_"+pid).html(num_s);
				$("#savestockimg_"+pid).data("prostock",num_s);
			};
		});
		$("#div_out3").hide();
		$("#btn_changestock").removeAttr("disabled");
	});
});
